package com.buyantech.smweb.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javacommon.util.DateTimeUtil;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.buyantech.smweb.dao.UserDao;
import com.buyantech.smweb.model.AutoPater;
import com.buyantech.smweb.model.User;
import com.buyantech.smweb.service.AutoPaterManager;
import com.buyantech.smweb.service.UserManager;

public class TestServlet extends HttpServlet {

	private Connection conn = null;
	PreparedStatement statement = null;

	public TestServlet() {
	}

	public void init(ServletConfig servletConfig) throws ServletException {
	}

	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException, ServletException {
		String str = req.getQueryString();
		if (str != null && !str.equals("")) {
			connSQL();
			ResultSet rs = selectSQL("select * from users where userid='" + str
					+ "'");
			try {
				if (rs.next()) {
					insertSQL("insert into autopater(id,paterId,ip,crdate)values('"+(new java.sql.Timestamp(System.currentTimeMillis())).toString()+"','"
							+ rs.getString("id") + "','" + req.getRemoteAddr()
							+ "','" + DateTimeUtil.getDateTimeString() + "')");
				}
			} catch (SQLException e) {
				System.out.println("显示时数据库出错。");
				e.printStackTrace();
				
			} catch (Exception e) {
				System.out.println("显示出错。");
				e.printStackTrace();
			}

		}
		deconnSQL();
		//
		try {
			resp.sendRedirect("http://www.jizhuan.cc");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		System.out.println("TestServlet init2");
	}

	public void destroy() {
	}

	void connSQL() {
		String url = "jdbc:mysql://localhost:3306/pay?characterEncoding=UTF-8";
		String username = "root";
		String password = "root"; // 加载驱动程序以连接数据库
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection(url, username, password);
		}
		// 捕获加载驱动程序异常
		catch (ClassNotFoundException cnfex) {
			System.err.println("装载 JDBC/ODBC 驱动程序失败。");
			cnfex.printStackTrace();
		}
		// 捕获连接数据库异常
		catch (SQLException sqlex) {
			System.err.println("无法连接数据库");
			sqlex.printStackTrace();
		}
	}

	void deconnSQL() {
		try {
			if (conn != null)
				conn.close();
		} catch (Exception e) {
			System.out.println("关闭数据库问题 ：");
			e.printStackTrace();
		}
	}

	// execute selection language
	ResultSet selectSQL(String sql) {
		ResultSet rs = null;
		try {
			statement = conn.prepareStatement(sql);
			rs = statement.executeQuery(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return rs;
	}

	boolean insertSQL(String sql) {
		try {
			statement = conn.prepareStatement(sql);
			statement.executeUpdate();
			return true;
		} catch (SQLException e) {
			System.out.println("插入数据库时出错：");
			e.printStackTrace();
		} catch (Exception e) {
			System.out.println("插入时出错：");
			e.printStackTrace();
		}
		return false;
	}
}
